package org.demo.treeeditor;

import java.awt.GraphicsEnvironment;
import java.util.ArrayList;
import java.util.List;

import org.boxlayout.gui.BoxLayoutEngine;
import org.boxlayout.gui.BoxLayoutFrame;
import org.boxlayout.gui.VisualElement;
import org.boxlayout.gui.layout.LayoutFactory;
import org.boxlayout.gui.utils.VisualElementXmlLoader;
import org.boxlayout.input.InputEvent;
import org.boxlayout.input.InputManager;

public class TreeEditor {
	BoxLayoutFrame blf;
	BoxLayoutEngine ble;
	StateMachine sm;
	//KeyboardManager km;
	TextPrompt tp;

	//Node rootNode;
	VisualElement rootVisualElement;
	HelpDisplayer hd;
	VisualElementXmlLoader vexl;

	public TreeEditor() {


		//displayAvailableFont();

		blf = new BoxLayoutFrame();	
		ble = blf.getBoxLayoutEngine();
		vexl = new VisualElementXmlLoader(ble);
		sm = new StateMachine();
		//km = new KeyboardManager();
		tp = new TextPrompt();
		hd = new HelpDisplayer();

		rootVisualElement = ble.createElement().setPreferedWidth(1.0f).setPreferedHeight(1.0f)
		.setId(-1).setAlpha(0.0f);
		;
		ble.setRootElement(rootVisualElement);
		ble.setMouseEventHandling(false);
		ble.setOrtho(false);
		ble.setVisualElementXmlLoader(vexl);
		
		//NodeFactory.getInstance().init(ble);
		//NodeFactory.getInstance().setTemplate(DomManager.loadFile("demo/data/treeEditor/node.bl.xml").getDocumentElement());

		VisualElement cameraRoot = ble.createElement().setPreferedWidth(1.0f).setPreferedHeight(1.0f)
		.setLayout(LayoutFactory.getInstance().getVerticalLayout()).setAlpha(0.0f)
		//.setText("ok")
		.setId(-1);
		//cameraRoot.add(ble.createElement().setText("Ab").setPadding(0.1f).setPreferedWidth(0.2f).setPreferedHeight(0.2f).setColor(ColorEnum.GREEN).setAlpha(0.5f));
		//cameraRoot.add(ble.createElement().setText("Az").setPadding(0.1f).setPreferedWidth(0.2f).setPreferedHeight(0.2f).setColor(ColorEnum.GREEN).setAlpha(0.5f));
		ble.getCamera().setRootElement(cameraRoot);
		hd.init(ble,cameraRoot);
		List<String> str = new ArrayList<String>();
		str.add("E edit");
		hd.show(str);


		//km.setBoxLayoutEngine(ble);
		//km.setTextPrompt(tp);

		tp.setBoxLayoutEngine(ble);

		sm.setBoxLayoutEngine(ble);
		sm.setTextPrompt(tp);
		sm.setHelpDisplayer(hd);
		//sm.setKeyboardManager(km);

		tp.init();


		Node root = createNode();
		root.setText("RootNode");
		sm.setRootNode(root);

		rootVisualElement.add(sm.getVisual());
		rootVisualElement.add(tp.getVisual());




		Node firstChild = createNode();
		firstChild.setText("firstNode");
		//rootNode.appendChild(firstChild);



		ble.reLayout();
		sm.setCurrentNode(root);
		centerCameraOn(root);


		sm.init();
		initInputDevices();

		System.out.println(rootVisualElement.serializeToText(""));
		blf.start();
	}



	private void initInputDevices() {
		InputManager im = new InputManager();
		im.listAvailableInputNames();
		List<String> l = new  ArrayList<String>();
		l.add("3Dconnexion SpaceNavigator");
		l.add("Logitech Logitech RumblePad 2 USB");
		String selected = im.setDefaultFromPriorityList(l);
		if(selected==null){
			System.out.println("No input Controller defined");
		}else{
			System.out.println("Input Controller selected : "+selected);
			if(selected.equals("3Dconnexion SpaceNavigator")){
				im.addEventListener(4,false, new InputEvent() {
					@Override public void onEvent(Float value) {
						if(value!=0f){
							ble.getCamera().moveZ(value*-0.005f);
						}
					}
				});
			}
			if(selected.equals("Logitech Logitech RumblePad 2 USB")){
				im.addEventListener(13,false, new InputEvent() {
					@Override public void onEvent(Float value) {
						if(0.2<value){
							ble.getCamera().moveZ(value*-1f);
						}else if(value<-0.2){
							ble.getCamera().moveZ(value*-1f);
						}
					}
				});

			}
			im.start();
		}


	}



	void displayAvailableFont() {
		GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
		String[] fontnames = e.getAvailableFontFamilyNames();
		System.out.println("\nFonts available on this platform: ");
		for (int i = 0; i < fontnames.length; i++)
			System.out.print(fontnames[i]);
	}



	public Node createNode(){
		//Node result = NodeFactory.getInstance().createNode();
		Node result = new Node(ble);
		//result.getVisual().finalizeLayout();
		return result;
	}

	/*public void setRoot(Node aNode){
		//rootVisualElement.getChilds().clear();
		rootVisualElement.add(aNode.getVisual());
		rootNode = aNode;
		rootNode.setRoot(true);
	}*/

	public void centerCameraOn(Node node){
		ble.getCamera().centerOn(node.getVisual());
	}

	public static void main(String[] args) {
		new TreeEditor();
	}
}
